/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package view.additional;

import com.sun.lwuit.Component;
import com.sun.lwuit.Form;
import com.sun.lwuit.Label;
import core.Settings;
import utils.Utils;

/**
 *
 * @author Kvest
 */
public class ChernovtsiCreator extends Creator
{
    //private static final int DEFAULT_LABEL_HEIGHT = 23;
    
    private Label fromLabel;
    private OrderDetailsText from;
    private OrderDetailsText region;
    private Label toLabel;
    private OrderDetailsText to;
    private OrderDetailsText autoClass;
    private OrderDetailsText fare;
    private Label commentsLabel; 
    private OrderDetailsText comments;
    private Label additionalLabel;
    private OrderDetailsText additional;
    private OrderDetailsText status;
    private OrderDetailsText time;
    
    boolean addIDAndDispetcher;
    private Label orderIdLabel;
    private OrderDetailsText orderId;
    private Label dispetcherNameLabel;
    private OrderDetailsText dispetcherName;
    
    private Label emptyLabel;
    private Label emptyLabel1;
    
    private int bottomMargin;
    private int textHeight;
    
    //private Container container; 

    public ChernovtsiCreator(Form owner, boolean addIDAndDispetcher)
    {
        super(owner);
        
        this.addIDAndDispetcher = addIDAndDispetcher;
    }

    public void init()
    {
        owner.setCyclicFocus(false);

        int ownerWidth = owner.getWidth();
        //container = new Container();
        
        fromLabel = new Label("Откуда");
        fromLabel.setEndsWith3Points(true);
        initMarging(fromLabel);
        
        from = new OrderDetailsText();
        if (from.getWidth() == 0) {
            from.setWidth(ownerWidth); 
        }
        
        region = new OrderDetailsText();
        if (region.getWidth() == 0) {
            region.setWidth(ownerWidth); 
        }
        
        toLabel = new Label("Куда");
        toLabel.setEndsWith3Points(true);
        initMarging(toLabel);
        
        to = new OrderDetailsText();
        if (to.getWidth() == 0) {
            to.setWidth(ownerWidth); 
        }
        
        autoClass = new OrderDetailsText();
        if (autoClass.getWidth() == 0) {
            autoClass.setWidth(ownerWidth); 
        }
        
        fare = new OrderDetailsText();
        if (fare.getWidth() == 0) {
            fare.setWidth(ownerWidth); 
        }
        
        commentsLabel = new Label("Примечание");
        commentsLabel.setEndsWith3Points(true);
        initMarging(commentsLabel);
        
        comments = new OrderDetailsText();  
        if (comments.getWidth() == 0) {
            comments.setWidth(ownerWidth); 
        }
        
        additionalLabel = new Label("Дополнительно");
        additionalLabel.setEndsWith3Points(true);
        initMarging(additionalLabel);
        
        additional = new OrderDetailsText();
        if (additional.getWidth() == 0) {
            additional.setWidth(ownerWidth); 
        }
        
        status = new OrderDetailsText();
        if (status.getWidth() == 0) {
            status.setWidth(ownerWidth); 
        }
        
        time = new OrderDetailsText();
        if (time.getWidth() == 0) {
            time.setWidth(ownerWidth); 
        }
        
        orderIdLabel = new Label("ID заказа");
        orderIdLabel.setEndsWith3Points(true);
        initMarging(orderIdLabel);
        
        orderId = new OrderDetailsText();
        if (orderId.getWidth() == 0) {
            orderId.setWidth(ownerWidth); 
        }
        
        dispetcherNameLabel = new Label("Диспетчер");
        dispetcherNameLabel.setEndsWith3Points(true);
        initMarging(dispetcherNameLabel);
        
        dispetcherName = new OrderDetailsText();
        if (dispetcherName.getWidth() == 0) {
            dispetcherName.setWidth(ownerWidth); 
        }

        emptyLabel = new Label(" ");
        emptyLabel.setSelectedStyle(emptyLabel.getUnselectedStyle());
        emptyLabel.setFocusable(true);
        
        emptyLabel1 = new Label(" ");
        
        bottomMargin = emptyLabel1.getUnselectedStyle().getMargin(Component.BOTTOM);
        textHeight = Settings.getSettings().getOrderTextHeight();
       
        //Добавляем все компоненты
        addAllComponents();
    }

    private void addAllComponents()
    {
      /*  container.addComponent(fromLabel);
        container.addComponent(from);
        container.addComponent(region);
        container.addComponent(toLabel);
        container.addComponent(to);
        container.addComponent(autoClass);
        container.addComponent(fare);
        container.addComponent(commentsLabel);
        container.addComponent(comments);  
        container.addComponent(additionalLabel);
        container.addComponent(additional);
        container.addComponent(status);
        container.addComponent(time);
        container.addComponent(orderIdLabel);
        container.addComponent(orderId);
        container.addComponent(dispetcherNameLabel);
        container.addComponent(dispetcherName);
        container.addComponent(emptyLabel);
        container.addComponent(emptyLabel1);
        owner.addComponent(container);*/
                    
        owner.addComponent(fromLabel);
        owner.addComponent(from);
        owner.addComponent(region);
        owner.addComponent(toLabel);
        owner.addComponent(to);
        owner.addComponent(autoClass);
        owner.addComponent(fare);
        owner.addComponent(commentsLabel);
        owner.addComponent(comments);  
        owner.addComponent(additionalLabel);
        owner.addComponent(additional);
        owner.addComponent(status);
        owner.addComponent(time);
        owner.addComponent(orderIdLabel);
        owner.addComponent(orderId);
        owner.addComponent(dispetcherNameLabel);
        owner.addComponent(dispetcherName);
        owner.addComponent(emptyLabel);
        owner.addComponent(emptyLabel1);
    }
     
    public void clear()
    { 
        //container.removeAll();
        
//        owner.removeAll();
        
        setInvisible(fromLabel);
        setInvisible(from);        
        setInvisible(region);     
        setInvisible(toLabel);
        setInvisible(to);
        setInvisible(autoClass);
        setInvisible(fare);
        setInvisible(commentsLabel);
        setInvisible(comments);
        setInvisible(additionalLabel);
        setInvisible(additional);
        setInvisible(status);
        setInvisible(time);
        setInvisible(orderIdLabel);
        setInvisible(orderId);
        setInvisible(dispetcherNameLabel);
        setInvisible(dispetcherName);
        setInvisible(emptyLabel);
        setInvisible(emptyLabel1);
    }
    
    private void initMarging(Component component) {
        component.getUnselectedStyle().setMargin(Component.TOP, 0);
        component.getSelectedStyle().setMargin(Component.TOP, 0);
        component.getUnselectedStyle().setMargin(Component.BOTTOM, 0);
        component.getSelectedStyle().setMargin(Component.BOTTOM, 0);
    }
    
    private void setInvisible(Component component)
    {
        component.setVisible(false);// componet.setText("");
        component.setPreferredH(0);   
    }
    
    private void setVisible(Component component, int linesCount)
    {
        //container.addComponent(componet);
        
       // owner.addComponent(componet);
        component.setPreferredH(textHeight * linesCount);
   //     componet.getUnselectedStyle().setMargin(Component.TOP, topMargin); - тормозит!!!
 //       component.getUnselectedStyle().setMargin(Component.BOTTOM, bottomMargin);
        component.setVisible(true);
    }
    
    public void setText(String value)
    {
        //Чистим форму
        clear();

        String[] items = Utils.split(value, "\n", true); 

        if (items.length < 9)
        {
            from.setText(value);
            setVisible(from, from.getLines());
            
            return;
        }
  
        //Откуда
        if (!items[0].equals(""))
        {
            //fromLabel.setText("Откуда");
            setVisible(fromLabel, 1);
            from.setText(Utils.replace(items[0], "|", "\n").trim());
            setVisible(from, from.getLines()); 
        }

        //Регион
        if (!items[1].equals(""))
        {
            region.setText(Utils.replace(items[1], "|", "\n").trim());
            setVisible(region, region.getLines());
        }

        //Куда
        if (!items[2].equals(""))
        {
            //toLabel.setText("Куда");
            setVisible(toLabel, 1);
            to.setText(Utils.replace(items[2], "|", "\n").trim());
            setVisible(to, to.getLines());
        }

        //Класс
        if (!items[3].equals(""))
        {
            autoClass.setText(Utils.replace(items[3], "|", "\n").trim());
            setVisible(autoClass, autoClass.getLines());
        }

        //Стоимость
        if (!items[4].equals(""))
        {
            fare.setText(items[4]);
            setVisible(fare, fare.getLines());
        }

        //Примечания
        if (!items[5].equals(""))
        {
            //commentsLabel.setText("Примечание");
            setVisible(commentsLabel, 1);
            comments.setText(Utils.replace(items[5], "|", "\n").trim());
            setVisible(comments, comments.getLines());
        }

        //Доп.услуги
        if (!items[6].equals(""))
        {
            //additionalLabel.setText("Дополнительно");
            setVisible(additionalLabel, 1);
            additional.setText(Utils.replace(items[6], "|", "\n").trim());
            setVisible(additional, additional.getLines());
        }

        //Статус
        if (!items[7].equals(""))
        {
            status.setText(items[7]);
            setVisible(status, status.getLines());
        }

        //Время        
        if (!items[8].equals(""))
        {
            time.setText(items[8]);
            setVisible(time, time.getLines());
        }
        
        //ID заказа и ФИО Диспетчера
        if (addIDAndDispetcher && items.length >= 11)
        {
            //orderIdLabel.setText("ID заказа");
            if (!items[9].equals("")) {
                setVisible(orderIdLabel, 1);
                orderId.setText(items[9]);
                setVisible(orderId, orderId.getLines());
            }
            
            if (!items[10].equals(""))
            {
                //dispetcherNameLabel.setText("Диспетчер");
                setVisible(dispetcherNameLabel, 1);
                dispetcherName.setText(Utils.replace(items[10], "|", "\n").trim());
                setVisible(dispetcherName, dispetcherName.getLines());
            }
        }

        //emptyLabel1.setText(" ");
        setVisible(emptyLabel1, 1);
        //emptyLabel.setText(" ");
        setVisible(emptyLabel, 1);
       
        owner.invalidate();
    }
}
